package jinyilw.swing.visuals.panel;

import jinyilw.swing.ColorTools;
import jinyilw.common.Operator;
import jinyilw.common.OperatorSets;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.i18n.IString;
import jinyilw.swing.visuals.Icomp.IButton;
import jinyilw.swing.visuals.comp.ColorComp;

import javax.swing.JComponent;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SimplePalette extends JComponent // 调色板
{
	private static final long serialVersionUID = -5071541789382604249L;
	public static final Color[] defaultColors = {Color.WHITE, Color.YELLOW,
			Color.ORANGE, Color.CYAN, new Color(200, 255, 0), Color.GREEN,
			Color.PINK, Color.MAGENTA, Color.RED, new Color(153, 153, 255),
			new Color(138, 43, 226), new Color(100, 0, 100),
			new Color(0, 153, 153), new Color(50, 150, 255), Color.BLUE,
			Color.GRAY, Color.BLACK};
	private Color currentColor = null;
	private Color[] colors;
	private boolean moreColorEnabled;
	private IButton moreColorButton;

	private final ColorCompsListener colorCompsListener = new ColorCompsListener();
	private final OperatorSets<Color> operatorSets = new OperatorSets<>();

	public SimplePalette()
	{
		super();
		initGUI();
	}

	private void initGUI()
	{
		setColumn(5);
		setColors(defaultColors);
	}

	public void setColors(Color[] colors)
	{
		if (colors == null || this.colors == colors)
			return;
		// Color[] oldColors = this.colors;
		if (moreColorEnabled)
			remove(moreColorButton);
		Component[] colorLabels = getComponents();
		if (colorLabels.length < colors.length)
		{
			for (int i = 0; i < colorLabels.length; i++)
				colorLabels[i].setBackground(colors[i]);
			for (int i = colorLabels.length; i < colors.length; i++)
			{
				ColorComp cLabel = new ColorComp(colors[i], true);
				cLabel.addMouseListener(colorCompsListener);
				add(cLabel);
			}
		} else
		{
			for (int i = 0; i < colors.length; i++)
				colorLabels[i].setBackground(colors[i]);
			for (int i = colors.length; i < colorLabels.length; i++)
				remove(colorLabels[i]);
		}
		if (moreColorEnabled)
			add(moreColorButton);
		this.colors = colors;
	}

	public void setColumn(int col)
	{
		setLayout(new GridLayout(0, col, 2, 2));
	}

	public void setMoreColorEnabled(boolean b)
	{
		if (moreColorEnabled == b)
			return;
		if (b)
		{
			getMoreColorButton();
			add(moreColorButton);
		} else
			remove(moreColorButton);
		moreColorEnabled = b;
	}

	public Color getCurrentColor()
	{
		return currentColor;
	}

	public IButton getMoreColorButton()
	{
		if (moreColorButton == null)
		{
			moreColorButton = new IButton();
			moreColorButton.addActionListener(new MoreColorActionListener());
			moreColorButton.setText("更多..");
			moreColorButton.setText(new ICSetting("more..", "更多..", "more.."),
					true);
			// moreColorButton.setToolTipText("自定义颜色");
			// moreColorButton.setPreferredSize(new Dimension(30, 25));
			moreColorButton.configAlpha(0.5f, 0.6f);
		}
		return moreColorButton;
	}

	@Override
	public void setEnabled(boolean b)
	{
		super.setEnabled(b);
		// moreColorButton.setEnabled(b);

	}

	public void addColorOperator(Operator<Color> operator)
	{
		operatorSets.add(operator);
	}

	private class ColorCompsListener extends MouseAdapter
	{
		@Override
		public void mouseClicked(final MouseEvent e)
		{
			if (isEnabled())
			{
				Color color = ((ColorComp) e.getSource()).getBackground();
				currentColor = color;
				moreColorButton.setBackground(null);
				operatorSets.operate(color);
			}
		}
	}

	private class MoreColorActionListener implements ActionListener
	{
		IString setCustomString = new IString("setCustom", "自定义颜色",
				"Set custom color");
		@Override
		public void actionPerformed(final ActionEvent e)
		{
			Color color = ColorTools.showDefaultColorDialog(null,
					setCustomString, currentColor);
			if (color != null)
			{
				currentColor = color;
				moreColorButton.setBackground(color);
				operatorSets.operate(color);
			}
		}
	}
}
